//index.js
//获取应用实例
import {
  Base64
} from 'js-base64'
const app = getApp()

Page({
  handletoken(){
    wx.login({
      success(res){
        console.log(res.code)
        wx.request({
          url: 'http://localhost:8099/v1/token',
          method: 'POST',
          data: {
            type: 100,
            account: res.code
          },
          success(data) {
            wx.setStorageSync('token', data.data.token)
          }
        })
      }
    })
    
  },

  handleVerifyToken(){
    wx.request({
      url: 'http://localhost:8099/v1/token/verify',
      method: 'POST',
      data: {
        token: wx.getStorageSync('token')
      },
      success(data) {
        if (data.data.is_valid) {
          console.log('token有效')
        }
      }
    })
  },

  handleAskMsg() {
    wx.request({
      url: 'http://localhost:8099/v1/goods/askGoodsMsg',
      method: 'POST',
      data: {
        goods_id: 1,
        content: '测试一下'
      },
      success(res) {
        console.log(res.data)
      },
      header: {
        Authorization: this._encode()
      }
    })
  },


  _encode() {
    // account:password
    // token
    // token:
    const token = wx.getStorageSync('token')
    const base64 = Base64.encode(token + ':')
    // Authorization:Basic base64(account:password)
    return 'Basic ' + base64
  }
})
